﻿using System;
using System.Data;
using System.Windows.Forms;
using CXMLStudioIDE.WinFormsUI.Docking;
using CXMLStudioIDE.Presenters;
using CXMLStudioIDE.Views;
using CXMLStudioIDE.Common.Entities;

namespace CXMLStudioIDE.Forms
{
    public partial class DataDictionaryForm : DockContent, IDataDictionaryView
    {
        #region Declarations

        private MainForm m_parent;
        private DataDictionaryPresenter m_presenter;
        private BusinessEntity m_be;
        private int m_ContextualDefinitionID;
        private int m_BusinessDefinitionID;
        private bool m_haschanged;
        private CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor htmlBusinessDef;
        private CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor htmlComments;
        private CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor htmlHistoricComments;

        #endregion

        #region Constructors
        
        public DataDictionaryForm(MainForm parent)
        {
            InitializeComponent();
            InitHtmlEditors();

            m_parent = (MainForm)parent;
            m_presenter = new DataDictionaryPresenter(this);
            this.FormClosing += new FormClosingEventHandler(OnFormClosing);
            this.htmlBusinessDef.chkEdit.CheckedChanged += chkEdit_CheckChanged;
            this.htmlComments.chkEdit.CheckedChanged += chkEdit_CheckChanged;
            this.htmlBusinessDef.saveButton.Click += BDsaveButtonClick;
            this.htmlComments.saveButton.Click += CommentsSaveButtonClick;
        }

        #endregion

        #region Methods
        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
            //this.htmlBusinessDef.Dispose();
            //this.htmlHistoricComments.Dispose();
            //this.htmlComments.Dispose();
        }
        public void FinalizeComponents()
        {
            this.htmlHistoricComments.CloseDocument();
            this.htmlComments.CloseDocument();
            this.htmlBusinessDef.CloseDocument();
            this.htmlBusinessDef = null;
            this.htmlComments = null;
            this.htmlHistoricComments = null;
        }
        private void InitHtmlEditors()
        {
            this.SuspendLayout();
            htmlComments = new CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor(false, false);
            htmlHistoricComments = new CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor(false, true);
            htmlBusinessDef = new CXMLStudioIDE.WinFormsUI.CXMLStudioControls.Editor(false, false);
            pnlNewComment.Controls.Add(htmlComments);
            pnlCommentHistory.Controls.Add(htmlHistoricComments);
            pnlBusinessDef.Controls.Add(htmlBusinessDef);
            this.ResumeLayout();        
        }

        public void chkEdit_CheckChanged(object o, EventArgs e)
        {
            CheckBox chk = o as CheckBox;
            if (!chk.Checked)
            {
                this.Save(false);
            }
            else
            {
                UpdateDataDictionary(BusinessEntity);
            }
        }

        public void BDsaveButtonClick(object o, EventArgs e)
        {
            SaveBusinessDefinition();
        }

        public void CommentsSaveButtonClick(object o, EventArgs e)
        {
            SaveComment();
        }
        #endregion
        
        #region IDataDictionaryView Members

        #region IDataDictionaryView Public Properties

        public new MainForm Parent
        {
            get
            {
                return m_parent;
            }
            set
            {
                m_parent = value;
            }
        }

        public string EntityName
        {
            get
            {
                return txtEntityName.Text;
            }
            set
            {
                txtEntityName.Text = value;
            }
        }

        public string Comments
        {
            get
            {
                return htmlComments.DocumentText;
            }
            set
            {
                htmlComments.DocumentText = value;
            }
        }

        public string HistoricComments
        {
            get
            {
                return htmlHistoricComments.DocumentText;
            }
            set
            {
                htmlHistoricComments.DocumentText = value;
            }
        }

        public string BusinessDefinition
        {
            get
            {
                return htmlBusinessDef.DocumentText;
            }
            set
            {
                htmlBusinessDef.DocumentText = value;
            }
        }

        public string BDBodyHtml
        {
            get
            {
                return htmlBusinessDef.BodyHtml;
            }
            set
            {
                htmlBusinessDef.BodyHtml = value;
            }            
        }
            
        public DataSet ds_BE_datadictionary
        {
            get
            {
                return dsBusinessEntityDataDictionary;
            }
            set
            {
                dsBusinessEntityDataDictionary = value;
            }
        }

        public string CommentBodyText
        {
            get 
            {
                return htmlComments.BodyText;
            }
            set
            {
                htmlComments.BodyText = value;
            }
        }

        public string BusinessDefBodyText
        {
            get
            {
                return htmlBusinessDef.BodyText;
            }
            set
            {
                htmlBusinessDef.BodyText = value;
            }
        }

        public int ContextualDefinitionID
        {
            get
            {
                return m_ContextualDefinitionID;
            }
            set
            {
                m_ContextualDefinitionID = value;
            }
        }

        public int BusinessDefinitionID
        {
            get
            {
                return m_BusinessDefinitionID;
            }
            set
            {
                m_BusinessDefinitionID = value;
            }
        }

        public BusinessEntity BusinessEntity
        {
            get
            {
                return m_be;
            }
            set
            {
                m_be = value;
            }
        }

        public bool HasChanged
        {
            get
            {
                return m_haschanged;
            }
            set
            {
                m_haschanged = value;
            }
        }

        public bool BDEditEnabled
        {
            get
            {
                return this.htmlBusinessDef.chkEdit.Checked;
            }
            set
            {
                this.htmlBusinessDef.chkEdit.Checked = value;
            }
        }

        public bool CommentEditEnabled
        {
            get
            {
                return this.htmlComments.chkEdit.Checked;
            }
            set
            {
                this.htmlComments.chkEdit.Checked = value;
            }
        }

        public bool BDSaveEnabled
        {
            get
            {
                return this.htmlBusinessDef.saveButton.Enabled;
            }
            set
            {
                this.htmlBusinessDef.saveButton.Enabled = value;
            }
        }

        public bool CommentSaveEnabled
        {
            get
            {
                return this.htmlComments.saveButton.Enabled;
            }
            set
            {
               this.htmlComments.saveButton.Enabled = value;
            }
        }
        #endregion

        #region IDataDictionaryView Methods

        public void ClearEditors()
        {
            htmlBusinessDef.Clear();
            htmlComments.Clear();
            htmlHistoricComments.Clear();
        }

        public void AddKeyWord(string sZ_Item)
        {
            lbKeywords.Items.Add(sZ_Item);
        }

        public void WriteEmtpyBDEditor()
        {
            htmlBusinessDef.Document.Write(String.Empty);
        }

        public void WriteEmptyHCEditor()
        {
            htmlHistoricComments.Document.Write(String.Empty);
        }

        public void UpdateDataDictionary(BusinessEntity be)
        {
            if (be != null)
            {
                m_be = be;
                m_presenter.UpdateDataDictionaryView(be);
            }
        }

        public void Save(bool SaveAndClose)
        {
            if (BDSaveEnabled)
            {
                SaveBusinessDefinition();
            }
            if (CommentSaveEnabled)
            {
                SaveComment();
            }
        }

        public void SaveComment()
        {
            m_presenter.SaveComment();
        }

        public void SaveBusinessDefinition()
        {
            m_presenter.SaveBusinessDefinition();
        }

        public void RemoveEmptyBDLinks()
        {
            this.htmlBusinessDef.RemoveEmptyLinks();
        }

        public void RemoveEmptyCommentLinks()
        {
            this.htmlComments.RemoveEmptyLinks();
        }

        #endregion

        #region IDataDictionaryViewEvents
        public new event FormClosingEventHandler OnFormClosing;
        #endregion

        #endregion
    }
}
